package com.example.translateappjava.Activity.HomeFragmentActivity;

import android.annotation.SuppressLint;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.translateappjava.R;
import com.example.translateappjava.Singleton.GlobalData;

public class SelectActivity extends AppCompatActivity {

    private SharedPreferences sharedPreferences;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_select);

        // 初始化SharedPreferences
        sharedPreferences = getSharedPreferences("checkbox_prefs", MODE_PRIVATE);

        // 初始化GlobalData的globalVariable
        GlobalData globalData = GlobalData.getInstance();
        globalData.initialize(this);  // 调用初始化方法

        // 设置窗口 Insets
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        // 获取控件
        CheckBox checkBox1 = findViewById(R.id.checkBox1);
        CheckBox checkBox2 = findViewById(R.id.checkBox2);
        CheckBox checkBox3 = findViewById(R.id.checkBox3);
        CheckBox checkBox4 = findViewById(R.id.checkBox4);
        CheckBox checkBox5 = findViewById(R.id.checkBox5);
        EditText editText = findViewById(R.id.editTextText2);
        Button button = findViewById(R.id.button5);

        // 恢复checkbox状态和EditText内容
        checkBox1.setChecked(sharedPreferences.getBoolean("checkbox1", false));
        checkBox2.setChecked(sharedPreferences.getBoolean("checkbox2", false));
        checkBox3.setChecked(sharedPreferences.getBoolean("checkbox3", false));
        checkBox4.setChecked(sharedPreferences.getBoolean("checkbox4", false));
        checkBox5.setChecked(sharedPreferences.getBoolean("checkbox5", false));
        editText.setText(sharedPreferences.getString("editTextContent", "")); // 恢复EditText内容

        // 关闭活动的按钮点击事件
        findViewById(R.id.imageView11).setOnClickListener(v -> finish());

        // 按钮点击事件
        button.setOnClickListener(v -> {
            boolean c1 = checkBox1.isChecked();
            boolean c2 = checkBox2.isChecked();
            boolean c3 = checkBox3.isChecked();
            boolean c4 = checkBox4.isChecked();
            boolean c5 = checkBox5.isChecked();
            String text = String.valueOf(editText.getText());
            text = text + "和";

            if (c1) {
                text = text + "糖尿病人群和";
            }
            if (c2) {
                text = text + "高血压人群和";
            }
            if (c3) {
                text = text + "高胆固醇人群和";
            }
            if (c4) {
                text = text + "痛风人群和";
            }
            if (c5) {
                text = text + "减脂人群和";
            }

            text = text.substring(0, text.length() - 1);

            // 更新 GlobalData 并保存到 SharedPreferences
            globalData.setGlobalVariable(text);
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putBoolean("checkbox1", c1);
            editor.putBoolean("checkbox2", c2);
            editor.putBoolean("checkbox3", c3);
            editor.putBoolean("checkbox4", c4);
            editor.putBoolean("checkbox5", c5);
            editor.putString("editTextContent", editText.getText().toString());
            editor.putString("globalVariable", text); // 保存 globalVariable
            editor.apply();

            System.out.println(text);
            Toast.makeText(SelectActivity.this, "修改数据成功", Toast.LENGTH_SHORT).show();
        });
    }
}
